package com.drcoffee.drcoffeemall.util;

import com.drcoffee.drcoffeemall.entry.User;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class DiscountedPriceUtil {
    @NotNull
    public static BigDecimal getDiscountedPrice(BigDecimal originalPrice, User user) {

        BigDecimal discountOne = user.getDiscount_one();
        BigDecimal discountTwo = user.getDiscount_two();
        if (user.getPermission_id() != 1) {
            // 不是国内企业用户都不享受折扣
            discountOne = new BigDecimal(0.00);
            discountTwo = new BigDecimal(0.00);
        }
        // 计算第一个折扣后的价格
        BigDecimal firstDiscountedPrice = originalPrice.subtract(originalPrice.multiply(discountOne.divide(new BigDecimal(100))));
        // 计算第二个折扣后的价格
        BigDecimal finalDiscountedPrice = firstDiscountedPrice.subtract(firstDiscountedPrice.multiply(discountTwo.divide(new BigDecimal(100))));
        return finalDiscountedPrice.setScale(2, RoundingMode.HALF_UP);
    }
}
